/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vnetlab.ui;

import com.mxgraph.io.mxCodec;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxGraph;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JOptionPane;
import vnetlab.ui.GraphEditor;
import vnetlab.ui.editor.EditorActions.EditAction;

/**
 *
 * @author ROGERIO
 */
public class CustomGraphComponent extends mxGraphComponent
{

        /**
         *
         */
        private static final long serialVersionUID = -6833603133512882012L;

        /**
         *
         * @param graph
         */
        public CustomGraphComponent(mxGraph graph)
        {
                super(graph);

                // Sets switches typically used in an editor

                setGridVisible(true);
                setToolTips(true);
                getConnectionHandler().setCreateTarget(true);

                //Não permite aumentar o tamanho do objeto
                graph.setCellsResizable(false);

                //Não permite aumentar o tamanho do objeto
                graph.setCellsEditable(false);

                //Não permite que uma lina exista sem ser conectando dois elementos
                graph.setCellsDisconnectable(false);
                
                //Ativa modo de conexão entre os elementos
                getConnectionHandler().setHandleEnabled(true);

                //Não permite a clonagem de elementos
                graph.setCellsCloneable(true);

                //Ativa a propagação de eventos
                graph.setEventsEnabled(true);
                graph.getView().setEventsEnabled(true);

                //Trata dos eventos realizados no editor
                graph.addListener(null, new CustomEventListner());


                graph.getSelectionModel().addListener(null, new EditAction());


                //graph.getModel().addListener(null, new CustomEventListner());

//                graph.getView().setEventsEnabled(true);
                graph.getView().addListener(null, new CustomEventListner());
                

                // Loads the defalt stylesheet from an external file
                mxCodec codec = new mxCodec();
                org.w3c.dom.Document doc = mxUtils.loadDocument(GraphEditor.class.getResource("/vnetlab/ui/resources/default-style.xml").toString());
                codec.decode(doc.getDocumentElement(), graph.getStylesheet());

                // Sets the background to white
                getViewport().setOpaque(false);
                setBackground(Color.WHITE);
        }
}
